﻿function AnalyseScater(container) {
    this.timepoints = [];
    this.floatingInForecastModel1 = [];
    this.floatingInForecastModel2 = [];
    this.floatingInForecastModel3 = [];
    this.floatingInForecastModel4 = [];
    this.regressionDatas = [];

    this.series = [];
    this.container = container;
}


AnalyseScater.prototype.tooltipFormatter = function (tooltip) {
    var ret = '<middle>' + tooltip.x + '</middle><br>';
    ret += '<table>';

    Highcharts.each(tooltip.points, function (point) {
        var series = point.series;
        ret += '<tr><td><span style="color:' + series.color + '">\u25CF</span> ' + series.name + '</td><td>' + Highcharts.pick(point.point.value, point.y) + '</td></tr>';
    });

    ret += '</table>';

    return ret;


};


AnalyseScater.prototype.getChartOptions = function () {
    var _ = this;
    return {
        chart: {
            renderTo: _.container,
            height: 300
        },
        title: {
            text: '空气质量等级模型预报离散图'
        },
        credits: {
            enabled: false
        },
        xAxis: {
            categories: _.timepoints,
            tickInterval: _.getStep(),
            title: {
                text: null
            }
        },
        yAxis: {
            alternateGridColor: '#FDFFD5',
            tickPixelInterval: 1,
            max: 5,
            min:-5,
            title: {
                text: null
            },
            labels: {
                style: {
                    fontSize: '10px'
                }
            }
        },
        tooltip: {
            shared: true
        },
        tooltip: {
            enabled: false
        },

        credits: {
            enabled: false
        },
        series: _.series
    }
};


AnalyseScater.prototype.getStep = function () {
    var _ = this,
        width = $("#" + _.container).width(),
        count = _.timepoints.length,
        num = 1,
        xWidth = 60;
    while (xWidth * count > width) {
        num = num * 2;
        count = count / 2;
    }
    return num;
}

AnalyseScater.prototype.onChartLoad = function () {
};

AnalyseScater.prototype.createChart = function () {
    var _ = this;
    this.chart = new Highcharts.Chart(_.getChartOptions(), function (chart) {
        _.onChartLoad();
    });
};

AnalyseScater.prototype.initData = function (jsonData) {
    var _ = this;
    if (jsonData.length > 0) {
        var d = jsonData;
        for (var i = 0; i < jsonData.length; i++) {
            _.floatingInForecastModel1.push(d[i].FloatingInForecastModel1);
            _.floatingInForecastModel2.push(d[i].FloatingInForecastModel2);
            _.floatingInForecastModel3.push(d[i].FloatingInForecastModel3);
            _.floatingInForecastModel4.push(d[i].FloatingInForecastModel4);
            _.regressionDatas.push(0);
            _.timepoints.push(formatDate(GetTimeFromJsonString(d[i].TimePoint), "MM月DD日"))
        }
        _.series = _.getSeries(_.series);
        _.createChart();
    }
    else {
        return "no data";
    }
};

AnalyseScater.prototype.getSeries = function (series) {
    var _ = this, series = [];
    //series.push({
    //    type: 'line',
    //    name: '回归线',
    //    color: '#434348',
    //    data: _.regressionDatas,
    //    marker: {
    //        enabled: false
    //    },
    //    states: {
    //        hover: {
    //            lineWidth: 0
    //        }
    //    },
    //    enableMouseTracking: false
    //});
    series.push({
        type: 'scatter',
        name: '多元回归模型',
        color:"#7CB5EC",
        data: _.floatingInForecastModel1,
        marker: {
            radius: 4
        }
    });
    series.push({
        type: 'scatter',
        name: '同期回归模型',
        color: "#90ED7D",
        data: _.floatingInForecastModel2,
        marker: {
            radius: 4
        }
    });
    series.push({
        type: 'scatter',
        name: 'BP神经网络模型',
        color: "#F45B5B",
        data: _.floatingInForecastModel3,
        marker: {
            radius: 4
        }
    });
    series.push({
        type: 'scatter',
        name: '天气形势统计模型',
        color: "#D3BA77",
        data: _.floatingInForecastModel4,
        marker: {
            radius: 4
        }
    });

    return series;
}


